package com.boc.langxiang.ui.activity.me;

import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.ui.activity.BaseActivity;

/**
 * Created by admin on 2016/6/24.
 */
public class MeBaseToolBarActivity extends BaseActivity {

    private FrameLayout mContentView;
    private Toolbar mToolbar;
    private TextView mTitle;
    private View userView;

    private static int[] ATTRS = {
            android.support.v7.appcompat.R.attr.windowActionBarOverlay,
            android.support.v7.appcompat.R.attr.actionBarSize
    };

    protected Toolbar getmToolbar(){
        return mToolbar;
    }

    @Override
    public void setContentView(int layoutResID) {
        initContentView();
        initUserView(layoutResID);
        initToolBar(setToolBarBackgroundColor(),setToolBarBackImg(),setToolBarTitleColor());
        setContentView(mContentView);
        setSupportActionBar(mToolbar);
        onCreateCustomToolBar(mToolbar);
    }


    protected int setToolBarTitleColor() {
        return Color.WHITE;
    }

    @DrawableRes
    protected int setToolBarBackImg() {
        return R.drawable.icon_me_white_back;
    }

    protected int setToolBarBackgroundColor() {
        return Color.BLACK;
    }

    private void initContentView() {
        mContentView = new FrameLayout(mContext);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.setLayoutParams(params);
    }

    @SuppressWarnings("ResourceType")
    private void initUserView(int layoutResID) {
        userView = LayoutInflater.from(mContext).inflate(layoutResID, null);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(ATTRS);

        boolean overly = typedArray.getBoolean(0, false);

        int toolBarSize = (int) typedArray.getDimension(1,
                mContext.getResources().getDimension(R.dimen.abc_action_bar_default_height_material));
        typedArray.recycle();
        params.topMargin = overly ? 0 : toolBarSize;
        mContentView.addView(userView, params);
    }

    private void initToolBar(int color, @DrawableRes int backImg,int titleColor) {
        View toolBar = LayoutInflater.from(mContext).inflate(R.layout.toolbar_me_layout, mContentView);
        mToolbar = (Toolbar) toolBar.findViewById(R.id.tool_bar);
        mToolbar.setTitle("");
        mToolbar.setBackgroundColor(color);
        mToolbar.setNavigationIcon(backImg);
       // mToolbar.setNavigationIcon(R.drawable.icon_me_white_back);
        mTitle = (TextView) toolBar.findViewById(R.id.title_tv);
        mTitle.setTextColor(titleColor);
    }


    protected void onCreateCustomToolBar(Toolbar toolbar) {
        toolbar.setContentInsetsRelative(0, 0);
    }


    protected void setToolBarTitle(@StringRes int stringId) {
      //  setTitleTypeFace();
        mTitle.setText(stringId);
    }


    protected void setToolBarTitle(String title) {
       // setTitleTypeFace();
        mTitle.setText(title);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return false;
    }

 /*   *//**
     * 是否设置指定字体
     *
     * @return false为默认不设置，true表示设置为指定字体
     *//*
    protected boolean isSetTypeFace() {
        return false;
    }

    private void setTitleTypeFace() {
        if (isSetTypeFace()) {
            Typeface typeFace = Typeface.createFromAsset(mContext.getAssets(), "fonts/lang.ttf");
            mTitle.setTypeface(typeFace);
        }
    }*/

}
